#include "utils.h"

#include <QSize>
#include <QWidget>
#include <QWindow>
#include <QDebug>
#include <QApplication>
#include <QtX11Extras/QX11Info>
#include <X11/extensions/shape.h>

// 设置鼠标穿透
void Utils::passInputEvent(int wid)
{
    Q_ASSERT(qApp);
    qDebug() << "platformName:" << QApplication::platformName();

    if (QApplication::platformName() == "xcb") {
        // x11平台
        //const auto display = QX11Info::display();
        XRectangle *reponseArea = new XRectangle;
        reponseArea->x = 0;
        reponseArea->y = 0;
        reponseArea->width = 0;
        reponseArea->height = 0;

        XShapeCombineRectangles(QX11Info::display(), wid, ShapeInput, 0, 0, NULL, 0, ShapeSet, YXBanded);
        delete reponseArea;
    } else if (QApplication::platformName() == "wayland") {
        // wayland平台
        QWidget *widget = QWidget::find(static_cast<WId>(wid));
        if(widget && widget->windowHandle()) {
            widget->windowHandle()->setMask(QRegion(0, 0, 1, 1));
        }
    }
}

// 取消鼠标穿透
void Utils::unPassInputEvent(int wid, QSize size)
{
    Q_ASSERT(qApp);
    qDebug() << "platformName:" << QApplication::platformName();

    if (QApplication::platformName() == "xcb") {
        // x11平台
        XRectangle *reponseArea = new XRectangle;
        reponseArea->x = 0;
        reponseArea->y = 0;
        reponseArea->width = size.width();
        reponseArea->height = size.height();

        XShapeCombineRectangles(QX11Info::display(), wid, ShapeInput, 0, 0, reponseArea ,1 ,ShapeSet, YXBanded);
        delete reponseArea;
    } else if (QApplication::platformName() == "wayland") {
        // wayland平台
        QWidget *widget = QWidget::find(static_cast<WId>(wid));
        if(widget && widget->windowHandle()) {
            widget->windowHandle()->setMask(QRegion(0, 0, size.width(), size.height()));
        }
    }
}
